package com.pkk.cloud.support.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRoleDelReqDto;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRoleIdReqDto;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRolePageQueryReqDto;
import com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRoleAllRespDto;
import com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRolePageRespDto;
import com.pkk.cloud.support.admin.entity.resource.PenguinSysRole;
import com.pkk.cloud.support.admin.mybatis.resource.PenguinSysRoleMapper;
import com.pkk.cloud.support.admin.service.PenguinSysRoleService;
import com.pkk.components.rpc.page.Page;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


@Service
public class PenguinSysRoleServiceImpl extends ServiceImpl<PenguinSysRoleMapper, PenguinSysRole> implements
    PenguinSysRoleService {


  /**
   * @Description: 根据用户获取角色
   * @Param: userId
   * @return: java.util.List<java.lang.String>
   * @Author: peikunkun
   * @Date: 2020/3/18 下午8:49
   */
  @Override
  public List<String> findUserRoleByUserId(long userId) {
    List<PenguinSysRole> list = this.findPenguinUserRoleByUserId(userId);
    return list.stream().map(PenguinSysRole::getRoleName).collect(Collectors.toList());
  }

  /**
   * @Description: 依旧用户id获取角色信息
   * @Param: userId
   * @return: java.util.List<com.pkk.cloud.support.admin.entity.resource.PenguinSysRole>
   * @Author: peikunkun
   * @Date: 2020/3/18 下午8:50
   */
  private List<PenguinSysRole> findPenguinUserRoleByUserId(long userId) {
    return baseMapper.findPenguinUserRoleByUserId(userId);
  }

  /**
   * @Description: 保存角色信息
   * @Param: role
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 下午 2:26
   */
  @Override
  public boolean saveSysRole(PenguinSysRole role) {
    return baseMapper.insert(role) == 1;
  }

  /**
   * @Description: 删除角色依据id
   * @Param: id
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 下午 2:29
   */
  @Override
  public boolean delSysRoleById(OauthRoleDelReqDto oauthRoleDelReqDto, String userId) {
    Long[] ids = oauthRoleDelReqDto.getId();
    for (int i = 0; i < ids.length; i++) {
      baseMapper.updateById(PenguinSysRole.builder().isDisabled(false).updateTime(new Date()).updateName(userId).id(ids[i]).build());
    }
    return true;
  }

  /**
   * @Description: 更新角色依据id
   * @Param: role
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 下午 2:31
   */
  @Override
  public boolean updateSysRoleById(PenguinSysRole role) {
    return baseMapper.updateById(role) == 1;
  }


  /**
   * @Description: 获取所有角色列表
   * @return: java.util.List<com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRoleAllRespDto>
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 下午 2:37
   */
  @Override
  public List<OauthSysRoleAllRespDto> findSysRoleList() {
    return baseMapper.findSysRoleList();
  }

  /**
   * @Description: 分页查询角色信息
   * @Param: page
   * @Param body
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 下午 2:43
   */
  @Override
  public PageInfo<OauthSysRolePageRespDto> findSysRoleListPage(Page page, OauthRolePageQueryReqDto body) {
    PageHelper.startPage(page.getPageIndex(), page.getPageSize());
    return new PageInfo<OauthSysRolePageRespDto>(baseMapper.findSysRoleListPage(body));
  }

  /**
   * @Description: 获取角色信息依据主键
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRolePageRespDto>
   * @Author: peikunkun
   * @Date: 2020/4/8 0008 下午 5:50
   */
  @Override
  public OauthSysRolePageRespDto findOauthRoleById(OauthRoleIdReqDto body) {
    final PenguinSysRole role = baseMapper.selectById(body.getId());
    final OauthSysRolePageRespDto oauthSysRolePageRespDto = new OauthSysRolePageRespDto();
    BeanUtils.copyProperties(role,oauthSysRolePageRespDto);
    return oauthSysRolePageRespDto;
  }
}
